package com.ncs.cns.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;

import com.ncs.cns.R;
import com.ncs.cns.business.constants.MethodConstant;
import com.ncs.cns.business.dao.impl.UserSessionDAO;
import com.ncs.cns.business.db.DBHelper;
import com.ncs.cns.business.http.HttpHelper;
import com.ncs.cns.business.to.TransferObject;
import com.ncs.cns.business.to.UserSessionInfo;
import com.ncs.cns.business.util.DateUtil;
import com.ncs.cns.business.util.JsonConvertor;
import com.ncs.cns.framework.Constant;

public class MainActivity extends BaseActivity {

	private HttpHelper httpHelper;

	private UserSessionDAO dao;

	private DBHelper dbHelper;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		if (application.isFirstStart()) {			
			SharedPreferences sp = getSharedPreferences(Constant.SHAREPATH,
					Context.MODE_PRIVATE);
			Editor editor = sp.edit();
			editor.putBoolean(Constant.FIRSTSTART, false);
			editor.commit();
		}
		dao = new UserSessionDAO(getBaseContext());
		httpHelper = new HttpHelper(this);
		dbHelper = new DBHelper(getBaseContext());
		new AutoLoginTask().execute();
	}

	private void startNewActivity() {
		Intent intent = null;
		// 暂时直接登陆
		// if(application.getUser() == null){
		// intent = new Intent(this, LoginActivity.class);
		// }else{
		intent = new Intent(this, CircuSeeActivity.class);
		// }
		startActivity(intent);
		this.finish();
	}

	class AutoLoginTask extends AsyncTask<Void, Void, Boolean> {

		@Override
		protected Boolean doInBackground(Void... arg0) {
			if(!isNetworkAvailable()){
				//showToastShort(R.string.network_unavailable);
				return false;
			}
			String currentUserId = userService.getCurrentUser();
			String json = httpHelper.doPost(dbHelper.buildParamStr(MethodConstant.AUTO_LOGIN,currentUserId));
//			if (!StringUtils.isEmpty(json)) {
				TransferObject resule = (TransferObject) JsonConvertor
						.getInstance().convert2Object(json,
								TransferObject.class);
				if(resule!=null){
					UserSessionInfo userSession = new UserSessionInfo();
					userSession.setUserId(resule.getCurrentUserId());
					userSession.setJsessionId(resule.getCurrentSessionId());
					userSession.setCreatedTime(DateUtil.getCurrentDateStr());
					userSession.setUpdatedTime(DateUtil.getCurrentDateStr());
					userSession.setLastRefreshTime(DateUtil.getCurrentDateStr());
					dao.insert(userSession);
					
					if(!userService.hasUserId()){
						if(Constant.IS_TEST_ENVIRONMENT){
							//userService.addUserWithUserId("");
							userService.addUserWithUserId(resule.getCurrentUserId());
						}
						else{
							userService.addUserWithUserId(resule.getCurrentUserId());
						}
					}
					
				    return true;
				}
			return false;
		}

		@Override
		protected void onPostExecute(Boolean success) {
			if(!success)showToastShort(R.string.network_unavailable);
			startNewActivity(); //总是可以进入围观列表页面，无网络或未登陆成功也可以看本地的信息
			/*if (success) {
				startNewActivity();
			} else {
				Toast.makeText(getApplicationContext(), "发起围观失败,请检查你的网络或重试", Toast.LENGTH_SHORT).show();
			}*/
		}

	}
}
